#include "LifeBoard.h"

//creates the lifeboard object and initializes the gameboard found inside
LifeBoard::LifeBoard(int xLength, int yLength)
{
    //creates the first row of the array with int pointers
    gameBoard = new int * [xLength];

    //inserts an int array into each cell of the first row of the pointer
    for(int i = 0; i < xLength; i++){
      gameBoard[i] = new int[yLength];
    }

    //sets the limits for the array for future retrieval
    xRange = xLength;
    yRange = yLength;

    //resets the value of the game board to 0
    resetLifeBoard();
}

//deletes all dynamically created arrays and clears memory
LifeBoard::~LifeBoard()
{
    //deletes all of the dynamically created arrays in the first row of gameBoard
    for(int i = 0; i < this->getLifeBoardXRange(); i++){
      delete [] gameBoard[i];
    }

    //deletes the dynamically created first row removing the double pointer
    delete [] gameBoard;
}

//sets the value at the position passed to the value that is passed
void LifeBoard::setLifeBoard(bool alive, int xValue, int yValue){
    if(alive)
        gameBoard[xValue][yValue] = 1;
    else
        gameBoard[xValue][yValue] = 0;
}

//returns the x limit of the array defined in the constructor
int LifeBoard::getLifeBoardXRange(){
  return xRange;
}

//returns the value of the Y limit of the array defined in the constructor
int LifeBoard::getLifeBoardYRange(){
  return yRange;
}

//returns the value at the x and y values specified in the int array
int LifeBoard::getLifeBoardValue(int xValue, int yValue){
    ///@todo this causes SIGSEV fault.
  return gameBoard[xValue][yValue];
}

//sets all the values in the game board to zero.
void LifeBoard::resetLifeBoard(){

  for(int x = 0; x < xRange; x++){
    for(int y = 0; y < yRange; y++){
      gameBoard[x][y] = 0;
    }
  }

}

//takes values from the lifeboard being passed and places them into
//the lifeboard that called the function
void LifeBoard::copy(LifeBoard * oldLifeBoard){
  for(int x = 0; x < xRange; x++){
    for(int y = 0; y < yRange; y++){
      gameBoard[x][y] = oldLifeBoard->getLifeBoardValue(x, y);
    }
  }
}
